package com.eduhzy.ttw.clazz.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.di.component.DaggerEditGroupComponent;
import com.eduhzy.ttw.clazz.di.module.EditGroupModule;
import com.eduhzy.ttw.clazz.mvp.contract.EditGroupContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.EditGroupData;
import com.eduhzy.ttw.clazz.mvp.model.entity.ReviewGroupData;
import com.eduhzy.ttw.clazz.mvp.model.entity.ReviewStudentData;
import com.eduhzy.ttw.clazz.mvp.presenter.EditGroupPresenter;
import com.eduhzy.ttw.clazz.mvp.ui.adapter.GroupSectionAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-6 16:33:20
 */
@Route(path = RouterHub.CLAZZ_EDITGROUPACTIVITY)
public class EditGroupActivity extends BaseActivity<EditGroupPresenter> implements EditGroupContract.View {
    @Autowired(name = Constants.PARCELABLE_DATA)
    ReviewGroupData mBean;
    @Autowired(name = Constants.ROUTER_INTEGER)
    int teamType;
    @BindView(R2.id.et_title)
    EditText mEtTitle;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.btn_over)
    QMUIRoundButton mBtnOver;
    @BindView(R2.id.btn_save)
    QMUIRoundButton mBtnSave;
    @BindView(R2.id.emptyView)
    QMUIEmptyView mEmptyView;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    @Inject
    GroupSectionAdapter mAdapter;
    @Inject
    List<EditGroupData> mList;
    private StringBuilder studentIds;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerEditGroupComponent
                .builder()
                .appComponent(appComponent)
                .editGroupModule(new EditGroupModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_group_edit");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.clazz_activity_edit_group;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mEtTitle.setText(mBean.getTeamName());
        mRvList.setLayoutManager(mLayoutManager);
        mRvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            EditGroupData sectionData = mList.get(position);
            if (!sectionData.isHeader) {
                ReviewStudentData studentData = sectionData.t;
                studentData.setCheck(!studentData.isCheck());
                mAdapter.notifyItemChanged(position);
            }

        });
        getData();
    }

    private void getData() {
        if (ObjectUtils.isNotEmpty(mBean)) {
            mPresenter.getTeamData(mBean.getTeamId());
        }
    }

    @Override
    public void showLoading() {
        mEmptyView.show(true);
    }

    @Override
    public void hideLoading() {
        mEmptyView.hide();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update(EditGroupData data) {
        EditGroupData one = new EditGroupData(true, "本小组成员");
        mList.add(one);
        for (ReviewStudentData studentData : data.getMemberList()) {
            mList.add(new EditGroupData(studentData.setCheck(true)));
        }
        EditGroupData two = new EditGroupData(true, "未分组成员");
        mList.add(two);
        for (ReviewStudentData studentData : data.getUnGroupingList()) {
            mList.add(new EditGroupData(studentData.setCheck(false)));
        }
        EditGroupData three = new EditGroupData(true, "已分组成员");
        mList.add(three);
        for (ReviewStudentData studentData : data.getGroupingList()) {
            mList.add(new EditGroupData(studentData.setCheck(false)));
        }
        mAdapter.notifyDataSetChanged();
    }


    @OnClick({R2.id.btn_over, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_over) {
            RxUtil.showConfirmDialog(getActivity(), "", "小组即将解散，你确定吗？", new QMUIDialogAction.ActionListener() {
                @Override
                public void onClick(QMUIDialog dialog, int index) {
                    mPresenter.removeTeam(mBean.getTeamId());
                    dialog.dismiss();
                }
            });
        } else if (i == R.id.btn_save) {
            if (RxUtil.showEditTip(this, mEtTitle, mEtTitle.getHint().toString())) {
                return;
            }
            studentIds = new StringBuilder();
            for (EditGroupData groupData : mList) {
                if (!groupData.isHeader) {
                    if (groupData.t.isCheck())
                        studentIds.append(groupData.t.getStudentId() + ",");
                }
            }
            String teamName = mEtTitle.getText().toString();

            mPresenter.saveTeam(mBean.getClassId(), mBean.getTeamId(), teamType, teamName, studentIds.toString());

        }
    }
}
